﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace UDPChatRoomApp
{
    class FileTransfer
    {
        public static int SendData(Socket s, byte[] data)
        {
            int total = 0;
            int size = data.Length;
            int dataleft = size;
            int sent;

            while (total < size)
            {
                sent = s.Send(data, total, dataleft, SocketFlags.None);
                total += sent;
                dataleft -= sent;
            }

            return total;
        }

        public static byte[] ReceiveData(Socket s, int size)
        {
            int total = 0;
            int dataleft = size;
            byte[] data = new byte[size];
            int recv;
            while (total < size)
            {
                if(s.Connected == true)
                    recv = s.Receive(data, total, dataleft, SocketFlags.None);
                else
                {
                    data = null;
                    break;
                }
                if (recv == 0)
                {
                    data = null;
                    break;
                }

                total += recv;
                dataleft -= recv;
            }
            return data;
        }

        public static int SendVarData(Socket s, byte[] data)
        {
            int total = 0;                                              // 信息发送总量
            int size = data.Length;                                     // 信息长度
            int dataleft = size;                                        // 未发送量
            int sent;                                                   // 已发送量
            byte[] datasize = new byte[4];
            datasize = BitConverter.GetBytes(size);                     
            sent = s.Send(datasize);                                    // 发送信息总量

            while (total < size)
            {
                sent = s.Send(data, total, dataleft, SocketFlags.None); // 开始发送信息
                total += sent;
                dataleft -= sent;
            }

            return total;
        }

        public static byte[] ReceiveVarData(Socket s)
        {
            int total = 0;                                              // 信息长度
            int received = 0;                                           // 已接收量
            byte[] datasize = new byte[4];                              // 信息长度
            received = s.Receive(datasize, 0, 4, SocketFlags.None);     // 获取信息长度 
            int size = BitConverter.ToInt32(datasize, 0);               
            int dataleft = size;                                        // 未接收量
            byte[] data = new byte[size];                               // 建立缓冲区
            while (total < size)
            {
                received = s.Receive(data, total, dataleft, SocketFlags.None);
                if (received == 0)                                              // 接收失败,直接返回
                {
                    data = null;
                    break;
                }
                total += received;                                              // 持续接收，直至所有数据接收完毕
                dataleft -= received;
            }
            return data;
        }
    }
}
